package cn.tedu.controller;

import cn.tedu.sp01.entity.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.sp01.web.until.JsonResult;
import cn.tedu.sp01.web.until.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {

    @Autowired
    private ItemService itemService;
//    获取订单商品列表
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);
        if(Math.random()<0.9){
//            阻塞代码,随机 [0,5000)
            int i = new Random().nextInt(5000);
            log.info("阻塞时间 "+i);
            Thread.sleep(i);

        }
        return JsonResult.ok().data(items);
    }
    @PostMapping("/decreastNumber")
    public JsonResult<?> decreastNumber(@RequestBody List<Item> items){
       itemService.decreaseNumber(items);
        return JsonResult.ok().msg("减少库存成功");
    }
    @GetMapping("/favicon.ico")
    public void ico(){}


}
